﻿namespace WebSocket.App_Code.Model
{
    using System;
    using WebSocket.App_Code.Common;
    using System.Data;
    using System.Data.SQLite;
    public class Users
    {
        /// <summary>
        /// 用户ID
        /// </summary>
        public int UserID;

        /// <summary>
        /// 用户名
        /// </summary>
        public string UserName;

        /// <summary>
        /// 登陆时间
        /// </summary>
        public DateTime LoginTime;

        /// <summary>
        /// 登出时间
        /// </summary>
        public DateTime LoginOutTime;

        /// <summary>
        /// 状态
        /// </summary>
        public string RecordStatus;

        /// <summary>
        /// 增加用户
        /// </summary>
        /// <returns></returns>
        public void Add()
        {
            string sqlStr = "insert into Users (UserName,LoginTime) values(@UserName,@LoginTime);select last_insert_rowid()";
            SQLiteParameter[] sp = { 
                                       new SQLiteParameter("@UserName",DbType.String,12),
                                       new SQLiteParameter("@LoginTime",DbType.DateTime,20)
                                   };
            sp[0].Value = UserName;
            sp[1].Value = LoginTime;
            UserID = int.Parse(SQLiteHelper.ExecuteScalar(sqlStr, sp).ToString());
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <returns></returns>
        public int Delete()
        {
            //同时删除SocketMsg,MsgToUsers这表里面的数据
            string sqlStr = "update Users set RecordStatus='InActive',LoginOutTime=datetime('now','localtime') where UserID=@UserID";
            SQLiteParameter[] sp = { 
                                       new SQLiteParameter("@UserID",DbType.Int32)
                                   };
            sp[0].Value = UserID;
            return SQLiteHelper.ExecuteNonQuery(sqlStr, sp);
        }
    }
}